﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Documents;
using Speakup;

namespace SpeakupUI
{
    class MainWindow: Window 
    {
        private TabControl TabControl;

        private Grid VisualizerGrid;
        private ScrollViewer TimeSheetScrollView;
        private TimeSheet TimeSheet;
        private ControlPanel ControlPanel;
        private ClassPanelList ClassPanelList;
        private TodoPanel TodoPanel;

        private ClassPanelList LoadDefaultClasses()
        {
            ClassList classes = null;
            try
            {
                classes = ClassList.Load();
            }
            catch (Exception)
            {
                return new ClassPanelList();    
            }

            return ClassPanelList.FromClassList(classes);
        }

        public MainWindow()
        {
            ClassPanelList = LoadDefaultClasses();

            Title = "Speakup Visualizer";
            TimeSheet = new TimeSheet();

            foreach (ClassPanel panel in ClassPanelList.Panels)
            {
                TimeSheet.AddChildren(panel);
            }

            ControlPanel = new ControlPanel(ClassPanelList, TimeSheet);

            TabItem visualizerTab;
            {
                VisualizerGrid = new Grid();

                TimeSheetScrollView = new ScrollViewer();
                TimeSheetScrollView.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                TimeSheetScrollView.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
                TimeSheetScrollView.HorizontalContentAlignment = HorizontalAlignment.Left;
                //TimeSheetScrollView.Content = TimeSheet;
                
                ColumnDefinition coldef = new ColumnDefinition();
                coldef.Width = new GridLength(1,GridUnitType.Star);
                VisualizerGrid.ColumnDefinitions.Add(coldef);

                coldef = new ColumnDefinition();
                coldef.Width = GridLength.Auto;
                VisualizerGrid.ColumnDefinitions.Add(coldef);

                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = new GridLength(1,GridUnitType.Star);
                VisualizerGrid.RowDefinitions.Add(rowdef);                


                VisualizerGrid.Children.Add(ControlPanel);
                VisualizerGrid.Children.Add(TimeSheet);
                Grid.SetColumn(TimeSheet, 0);
                Grid.SetColumn(ControlPanel, 1);
                
                visualizerTab = new TabItem();
                visualizerTab.Header = "Visualizer";
                visualizerTab.Content = VisualizerGrid;
            }

            TabItem todoTab;
            {
                StackPanel tab2StackPanel = new StackPanel();

                TodoPanel= new TodoPanel(ClassPanelList);
                tab2StackPanel.Children.Add(TodoPanel);

                todoTab = new TabItem();
                todoTab.Header = "Todo List";
                todoTab.Content = tab2StackPanel;
            }

            TabControl = new TabControl();
            TabControl.Items.Add(visualizerTab);
            TabControl.Items.Add(todoTab);
            TabControl.TabStripPlacement = Dock.Bottom;
            TabControl.SelectionChanged += delegate {TodoPanel.Refresh();};

            Content = TabControl;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);

            ClassList classes = new ClassList();
            foreach (ClassPanel panel in ClassPanelList.Panels)
            {
                classes.Add(panel.SpeakupClass);
            }

            classes.Save();
        }
    }

}
